package com.esun.service.apiImpl;

import com.alibaba.excel.EasyExcel;
import com.esun.api.interfaces.IExampleApi;
import com.esun.api.request.ExampleRequestDto;
import com.esun.api.response.BaseQueryResponseDto;
import com.esun.api.response.ExampleResponseDto;
import com.esun.common.enums.Message;
import com.esun.common.response.RestResponse;
import com.esun.common.utils.MessageUtils;
import com.esun.dao.eo.ExampleEo;
import com.esun.service.service.ExampleService;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Service("ExampleApi")
public class ExampleApiImpl implements IExampleApi {
	@Resource
	ExampleService exampleService;

	@Override
	public RestResponse getExampleByPage(int pageIndex, int pageSize) {
		ExampleRequestDto requestDto = new ExampleRequestDto();
		requestDto.setPageIndex(pageIndex);
		requestDto.setPageSize(pageSize);
		PageInfo<ExampleResponseDto> pageInfo = exampleService.getExampleByPage(requestDto);
		String code = Message.QUERY_SUCCESS.getCode();
		String message = MessageUtils.getInfoByCode(code);
		BaseQueryResponseDto baseQueryResponseDto = new BaseQueryResponseDto();
		baseQueryResponseDto.setList(pageInfo.getList());
		baseQueryResponseDto.setCount(pageInfo.getTotal());
		return new RestResponse(code, message, baseQueryResponseDto);
	}



	@Override
	public RestResponse addExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleResponseDto> resultResponseDtoList = exampleService.addExample(requestDtoList);
		String code = Message.QUERY_SUCCESS.getCode();
		for (ExampleResponseDto responseDto : resultResponseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				code = Message.QUERY_ERROR.getCode();
				break;
			}
		}
		String message = Message.getInfoByCode(code);
		return new RestResponse(code, message, resultResponseDtoList);
	}

	@Override
	public RestResponse updateExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleResponseDto> resultResponseDtoList = exampleService.updateExample(requestDtoList);
		String code = Message.QUERY_SUCCESS.getCode();
		for (ExampleResponseDto responseDto : resultResponseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				code = Message.QUERY_ERROR.getCode();
				break;
			}
		}
		String message = Message.getInfoByCode(code);
		return new RestResponse(code, message, resultResponseDtoList);
	}

	@Override
	public RestResponse deleteExample(List<ExampleRequestDto> requestDtoList) {
		List<ExampleResponseDto> resultResponseDtoList = exampleService.deleteExample(requestDtoList);
		String code = Message.QUERY_SUCCESS.getCode();
		for (ExampleResponseDto responseDto : resultResponseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				code = Message.QUERY_ERROR.getCode();
				break;
			}
		}
		String message = Message.getInfoByCode(code);
		return new RestResponse(code, message, resultResponseDtoList);
	}

	@Override
	public RestResponse importExample(MultipartFile file) {
		List<ExampleEo> exampleEoList = Lists.newArrayList();
		try {
			exampleEoList = EasyExcel.read(file.getInputStream()).head(ExampleEo.class).sheet().doReadSync();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<ExampleResponseDto> responseDtoList = exampleService.importExample(exampleEoList);
		String code = Message.QUERY_SUCCESS.getCode();
		for (ExampleResponseDto responseDto : responseDtoList) {
			if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
				code = Message.QUERY_ERROR.getCode();
				break;
			}
		}
		String message = Message.getInfoByCode(code);
		return new RestResponse(code, message, responseDtoList);
	}

	@Override
	public void exportExample(ExampleRequestDto requestDto) {
		exampleService.exportExample(requestDto);
	}

	@Override
	public void downloadTemplate(String path) {
		exampleService.downloadTemplate(path);
	}
}